<template>
  <div class="app-container">

    <div style="padding: 5px;" />
    <el-button type="primary" size="small" @click="onclickAdd">添加分组</el-button>

    <div style="padding: 5px;" />
    <el-table :data="list" border row-key="id" align="left">
      <el-table-column label="编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="分组名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="分组人员" align="center">
        <template slot-scope="scope">
          {{ scope.row.names }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">

          <el-button type="primary" size="mini" plain @click="onClickModify(scope.$index)"> 编辑</el-button>
          <el-button type="danger" size="mini" plain @click="onClickDel(scope.row.id)"> 删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增分组" width="50%" :visible.sync="addDialog.visible">
      <el-form>
        <el-form-item label="名称">
          <el-input v-model="addDialog.name" />
        </el-form-item>

        <el-form-item label="人员列表">
          <div/>
          <el-checkbox-group v-model="addDialog.userlist">
            <el-checkbox v-for="(item,index) in users" :key="index" :label="item.uid">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="add">新 建</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改分组" width="50%" :visible.sync="modifyDialog.visible">
      <el-form>
        <el-form-item label="名称">
          <el-input v-model="modifyDialog.name" />
        </el-form-item>

        <el-form-item label="人员列表">
          <div/>
          <el-checkbox-group v-model="modifyDialog.userlist">
              <el-checkbox v-for="(item,index) in users" :key="index" :label="item.uid">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modifyDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="modify">修 改</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { websock } from '@/websock'
import { MessageBox } from 'element-ui'

export default {
  data() {
    return {
      list: null,
      users: [],
      addDialog: {
        visible: false,
        name: '',
        userlist: []
      },
      modifyDialog: {
        visible: false,
        id: 0,
        name: '',
        userlist: []
      }
    }
  },
  mounted() {
    this.fetchData()

    websock.getAllUsers().then(res => {
      this.users = res
      console.log('created', res)
    })
  },
  methods: {
    // 获取数据
    fetchData() {
      websock.getUserGroupList().then(data => {
        this.list = data
      })
    },
    // 点击新增
    onclickAdd() {
      this.addDialog.visible = true
      this.addDialog.name = ''
      this.addDialog.userlist = []
    },
    // 点击删除
    onClickDel(id) {
      MessageBox.confirm('是否删除', '是否删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        websock.delUserGroup({ id }).then(() => {
          this.fetchData()
        })
      })
    },
    // 点击修改
    onClickModify(idx) {
      this.modifyDialog.visible = true
      this.modifyDialog.id = this.list[idx].id
      this.modifyDialog.name = this.list[idx].name
      this.modifyDialog.userlist = this.list[idx].userlist
    },
    // 点击增加
    add() {
      console.log(this.addDialog)
      this.addDialog.visible = false
      websock.addUserGroup({
        name: this.addDialog.name,
        userlist: this.addDialog.userlist
      }).then(() => {
        this.fetchData()
      })
    },
    // 点击修改
    modify() {
      console.log(this.modifyDialog)
      this.modifyDialog.visible = false
      websock.modifyUserGroup({
        id: this.modifyDialog.id,
        name: this.modifyDialog.name,
        userlist: this.modifyDialog.userlist
      }).then(() => {
        this.fetchData()
      })
    }
  }
}
</script>
